
package com.winning.sx.microframework.controller.base;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.common.util.FileTools;
import com.winning.sx.microframework.common.util.FlowCodeUtil;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.service.EasyService;
import com.winning.sx.microframework.service.sysmanage.ConstService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/product/manage")
public class ProductManageController {
    @Autowired
    EasyService easyService;
    @Autowired
    ConstService service;
    @RequestMapping(value = {"/", ""})
    public ModelAndView init() throws IOException, CustomException, SQLException {
        ModelAndView mv = new ModelAndView("/base/productManage");
        List state= service.queryCount("200");
        mv.addObject("state",state);
        List productState= service.queryCount("202");
        mv.addObject("productState",productState);
        List indexState= service.queryCount("204");
        mv.addObject("indexState",indexState);
        List type= service.queryCount("301");
        mv.addObject("type",type);
        List brand= service.queryCount("300");
        mv.addObject("brand",brand);
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = {"/queryList"})
    public AjaxTableResponseMessage queryMessageList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) 
            param = objectMapper.readValue(datas, Map.class);
                Map paramNew = new HashMap();
                if("0".equals(param.get("types"))) {
                    paramNew.put("type",null);
                }else{
                    paramNew.put("type",param.get("types"));
                }
                if("0".equals(param.get("brands"))) {
                    paramNew.put("brand",null);
                }else{
                    paramNew.put("brand",param.get("brands"));
                }
            if("0".equals(param.get("indexState"))) {
                paramNew.put("indexState",null);
            }else{
                paramNew.put("indexState",param.get("indexState"));
            }
            if("0".equals(param.get("productState"))) {
                paramNew.put("productState",null);
            }else{
                paramNew.put("productState",param.get("productState"));
            }
                String sql="select p.*,c1.name brand,c2.name type from BASE_Product p " +
                        "left join BASE_Const c1 on c1.code=p.brandCode " +
                        "left join BASE_Const c2 on c2.code=p.typeCode where 1=1 and p.validFlag=100001"+
                        (paramNew.get("type") != null ? (" and p.typeCode='" + paramNew.get("type") + "'") : "") +
                        (paramNew.get("brand") != null ? (" and p.brandCode='" + paramNew.get("brand") + "'") : "") +
                        (paramNew.get("indexState") != null ? (" and p.indexState='" + paramNew.get("indexState") + "'") : "") +
                        (paramNew.get("productState") != null ? (" and p.productState='" + paramNew.get("productState") + "'") : "") +
                        " order by createTime desc";
                DataTable all = easyService.querySql(sql,paramNew);
                ajaxTableResponseMessage.setDatas(all.getDataRows());
                ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }
    @ResponseBody
    @RequestMapping(value = {"/save"})
    public AjaxDataResponseMessage insert(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                Map param=new HashMap();
                param.put("typeCode",map.get("typeCode"));
                param.put("brandCode",map.get("brandCode"));
                param.put("modelCode",map.get("modelCode"));
                param.put("productState",map.get("productState"));
                param.put("productState",map.get("productState"));
                param.put("describe",map.get("describe"));
                param.put("indexState",map.get("indexState"));
                param.put("id",map.get("id"));
                if("".equals(map.get("id").toString())){
                    String code=FlowCodeUtil.getFlowCode();
                    param.put("code",code);
                }
                if(!"".equals(map.get("picture1"))){
                    //图片流生成文件返回文件地址
                    param.put("picture1", FileTools.saveImage("productPicture",map.get("picture1")));
                }
                if(!"".equals(map.get("picture2"))){
                    //图片流生成文件返回文件地址
                    param.put("picture2", FileTools.saveImage("productPicture",map.get("picture2")));
                }
                if(!"".equals(map.get("picture3"))){
                    //图片流生成文件返回文件地址
                    param.put("picture3", FileTools.saveImage("productPicture",map.get("picture3")));
                }
                if(!"".equals(map.get("picture4"))){
                    //图片流生成文件返回文件地址
                    param.put("picture4", FileTools.saveImage("productPicture",map.get("picture4")));
                }
                if(!"".equals(map.get("picture5"))){
                    //图片流生成文件返回文件地址
                    param.put("picture5", FileTools.saveImage("productPicture",map.get("picture5")));
                }
                if(!"".equals(map.get("picture6"))){
                    //图片流生成文件返回文件地址
                    param.put("picture6", FileTools.saveImage("productPicture",map.get("picture6")));
                }
                easyService.save("BASE_Product", "id", param);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/delete"})
    public AjaxDataResponseMessage delete(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                easyService.delete("BASE_Product", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

}

